import Enumerable from 'linq';
import { toWords } from './engWords';

/**
 * 
 * @param {list to generate} list 
 * @param {type=1?simple:full} type 
 */
export function toItemDesc(list, type) {
    if (list.length <= 0) return null;

    // var nList = list.distinct(r => r.objectNumber);

    var groupSum = Enumerable.from(list).distinct(r => r.objectNumber).groupBy("$.boxTypeName", null,
        function (key, g) {
            var result = {
                boxType: key,
                count: g.count(),
                total: g.sum("$.qty")
            }
            return result;
        }).toArray();

    var str = '';
    var total = 0;
    groupSum.map(item => {
        str += item.count + '*' + item.boxType + '+';
        total += item.count;
    })

    var strSimple = toWords(total) + '(' + str.slice(0, str.length - 1) + ')';
    var strFull = toWords(total) + '(' + str.slice(0, str.length - 1) + ') container(s) only';

    return (type === "simple" ? strSimple : strFull).toUpperCase();
}